package com.ruoyi.business.service;

import com.ruoyi.business.User;
import com.ruoyi.business.UserCategory;
import com.ruoyi.business.mapper.UserCategoryMapper;
import com.ruoyi.business.vo.UserCategoryVO;
import com.ruoyi.common.common.CommonEnum;
import com.ruoyi.common.common.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @title: UserService
 * @copyright: copyright (c) 2021
 * @author: jiangguanjun
 * @date: 2023/2/8 12:55
 * @firstReview:
 * @lastReview:
 * @desc: 用户相关
 */
@Slf4j
@Component
public class UserCategoryService {

    @Autowired
    private UserCategoryMapper userCategoryMapper ;



    public ResultBody listAll(Long userId) {
        List<UserCategoryVO> userCategoryList = userCategoryMapper.listByUId(userId) ;
        return ResultBody.success(userCategoryList );
    }

    public ResultBody add(Long userId, Integer cId) {

        UserCategory userCategory = new UserCategory() ;
        userCategory.setcId(cId);
        userCategory.setuId(userId);
        // 如果查询已经添加则不再添加
        UserCategory userCategoryDB = userCategoryMapper.selectByCidAndUid(cId,userId) ;
        if(userCategoryDB != null){
            return ResultBody.success() ;
        }
        int res = userCategoryMapper.insertSelective(userCategory) ;
        if(res>0){
            return ResultBody.success() ;
        }

        return ResultBody.error(CommonEnum.USER_CATEGORY_ADD_ERROR) ;
    }

    public ResultBody delete(Integer id) {
        UserCategory userCategory = userCategoryMapper.selectByPrimaryKey(id) ;
        if(userCategory == null){
            return ResultBody.error(CommonEnum.USER_CATEGORY_FIND_ERROR) ;
        }

        int res = userCategoryMapper.deleteByPrimaryKey(id) ;
        if(res>0){
            return ResultBody.success() ;
        }

        return ResultBody.error(CommonEnum.USER_CATEGORY_DELETE_ERROR) ;
    }

    public List<User> findByVipAndCid(Integer vip, Integer cId) {
        return userCategoryMapper.findByVipAndCid(vip,cId) ;
    }
}
